package com.fix.exception;

import java.util.HashMap;
import java.util.Map;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 全局异常处理类
 * @author FX_PC
 *
 */

@ControllerAdvice
public class GlobalControllerAdvice {
	
	//处理空指针  --->json
	@ExceptionHandler(NullPointerException.class)
	@ResponseBody
	public Map handlerNulpoint(NullPointerException ex) {
		System.out.println("异常了 http://localhost/ssm/");
		Map map= new HashMap();
		map.put("code", 5000);
		map.put("msg", ex.getMessage());
		return map;
	}
	
	//我自己的业务异常 --> web页
	@ExceptionHandler(BizException.class)
	public ModelAndView handleCustomExp(BizException ex) {
		ModelAndView mv = new ModelAndView();
		mv.addObject("errmsg","自定义错误消息="+ex.getMessage());   //提示自己的错误消息
		mv.setViewName("bizerr");
		return mv;
	}
	
	//系统异常 --> web页
	@ExceptionHandler(Exception.class)
	public ModelAndView handleException(Exception ex) {
		ModelAndView mv = new ModelAndView();
		mv.addObject("errmsg","其他错误消息="+ex.getMessage());   //提示自己的错误消息
		mv.setViewName("bizerr");
		return mv;
	}

}
